package cn.wolfcode.crm.util;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class UnauthorizedExceptionUtil {

    @ExceptionHandler(UnauthorizedException.class)
    public void handler(HttpServletResponse response, HandlerMethod method) throws IOException {
        if (method.getMethodAnnotation(ResponseBody.class)!=null){
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().print(JSON.toJSONString(new JSONResult().mark("你沒有該權限操作")));
        }else {
            throw new UnauthorizedException();
        }
    }
}
